


riot.tag2('redux-counter', '<p>number: {data.n}</p> <p> <button type="button" onclick="{fn.add}">+</button> <button type="button" onclick="{fn.undo}">undo</button> </p>', '', '', function(opts) {
  var tag = this;
  var addAction = pip.redux.createAction('ADD', 'n', 'delay');
  var undoAction = pip.redux.createAction('UNDO');
  tag.data = opts.store.getState();
  tag.fn = {
    add: function(){
      opts.store.dispatch(addAction(1, 1000));
    },
    undo: function(){
      opts.store.dispatch(undoAction());
    }
  };
  tag.on('mount', function(){});
  tag.on('update', function(){
    tag.data = opts.store.getState();
  });
});



riot.tag2('comail-index', '<ul> <li><a href="#!mail">邮件管理</a></li> <li><a href="#!template">模版管理</a></li> </ul> <redux-counter store="{store}"></redux-counter>', '', '', function(opts) {
  var tag = this;
  var logger = function(store){
    return function(next){
      return function(action){
        var timer;
        tag.app.log('开始分发:', action);
        try{
          if(action.delay){
            timer = setTimeout(function(){
              next(action);
              tag.update();
              tag.app.log('分发后 state:', store.getState());
            }, action.delay);
          }
          else{
            next(action);
            tag.app.log('分发后 state:', store.getState());
          }
        } catch(err) {
          tag.app.log('分发失败:', action, err);
        }
        return function cancel(){
          clearTimeout(timer);
        }
      }
    }
  };
  var counterReducer = pip.redux.createReducer({n: 0}, {
    ADD: function(state, action){
      if(!state.past) state.past = [];
      state.past.push(state.n);
      state.n += action.n;
      return state;
    },
    UNDO: function(state, action){
      state.n = state.past.splice(state.past.length-1, 1).pop() || 0;
      return state;
    }
  });
  tag.store = Redux.createStore(counterReducer, Redux.applyMiddleware(logger));
  tag.fn = {};
  tag.on('mount', function(){
  });
});
